package com.hopu.dy.controller;

import com.hopu.dy.service.IDyService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("demo")
public class DemoController {
    @Resource
    private IDyService dyService;
    @RequestMapping("upload")
    public String upload(@RequestBody byte[] file, String fileName) throws IOException {
        System.out.println("文件名："+fileName+"，文件大小：" + file.length + "字节");
        String desturl ="D:/test/dy-de-v/entry/src/main/resources/rawfile/"+fileName;
        FileOutputStream fileOutputStream = new FileOutputStream(desturl);
        fileOutputStream.write(file);
        fileOutputStream.close();

//        dyService.insert(fileName);

        return "添加成功";
    }

    @GetMapping("file/{fileName}")
    public void show(@PathVariable("fileName") String fileName, HttpServletResponse response) throws IOException {
        //I流
        FileInputStream fileInputStream = new FileInputStream("D:/test/dy-de-v/entry/src/main/resources/rawfile/" + fileName);
        //O流
        ServletOutputStream outputStream = response.getOutputStream();

        //告诉客户端这是一个视频文件，不要启用下载
        response.setContentType("video/mp4");

        //IO读写
        byte[] data = new byte[1024];
        int len;
        while ((len = fileInputStream.read(data)) != -1) {
            outputStream.write(data, 0, len);
        }

        outputStream.close();
        fileInputStream.close();
    }


}